﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
using BankCharts.Silverlight.ViewModel.Models;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace BankCharts.Silverlight.ViewModel.SubViewModel
{
    public class StatisticsManagerVM : ViewModelBase
    {
        #region Categories
        private ObservableCollection<Category> _Categories = new ObservableCollection<Category>();

        /// <summary>
        /// Gets the Categories property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ObservableCollection<Category> Categories
        {
            get
            {
                return _Categories;
            }

            set
            {
                if (_Categories == value)
                {
                    return;
                }

                _Categories = value;

                // Update bindings, no broadcast
                RaisePropertyChanged("Categories");
            }
        }
        #endregion

        public void PrepareStatistics(IEnumerable<Category> categories, IEnumerable<BankLine> bankLines)
        {
            Categories.Clear();
            Category othercat = null;
            foreach (var category in categories)
            {
                category.AffectedBankLines.Clear();
            }

            bool affected = false;
            foreach (var itemBL in bankLines)
            {
                if (itemBL.Value < 0)
                {
                    affected = false;
                    foreach (var itemCat in categories)
                    {
                        string textLine = itemBL.Text.ToLower();
                        foreach (var item in itemCat.Tokens)
                        {
                            if (textLine.Contains(item.ToLower()))
                            {
                                itemCat.AffectedBankLines.Add(itemBL);
                                affected = true;
                                break;
                            }
                        }
                        if (affected)
                        {
                            break;
                        }
                    }
                    if (!affected)
                    {
                        if (othercat == null)
                        {
                            othercat = new Category(null);
                            othercat.Name = "Other";
                            
                        }
                        othercat.AffectedBankLines.Add(itemBL);
                    }
                }
            }

            foreach (var itemCat in categories)
            {
                if (itemCat.AffectedBankLines.Count > 0)
                {
                    Categories.Add(itemCat);
                }
            }
            if(othercat != null)
            {
                Categories.Add(othercat);    
            }
        }
    }
}
